package com.fengchao.api.service;

import com.fengchao.api.controller.v1.CfiController;
import com.fengchao.api.dao.cfg.*;
import com.fengchao.model.cfg.*;
import com.fengchao.model.cfi.CfiArtistCrowdfundingApply;
import com.fengchao.model.cfi.CfiConcertCrowdfundingPayBackItems;
import org.apache.ibatis.annotations.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by wangnian on 2016/6/6.
 */
@Service
public class CfgService {
    @Autowired
    CfgApiConfigMapper cfgApiConfigMapper;
    @Autowired
    CfgRegionMapper cfgRegionMapper;
    @Autowired
    CfgSongTypeMapper cfgSongTypeMapper;
    @Autowired
    CfgWebBannerMapper cfgWebBannerMapper;
    @Autowired
    CfgAppBannerMapper cfgAppBannerMapper;
    @Autowired
    CfgAppConfigMapper cfgAppConfigMapper;
    @Autowired
    CfgApplyCrowdfundingItemsMapper cfgApplyCrowdfundingItemsMapper;
    @Autowired
    CfgChinaCityListMapper cfgChinaCityListMapper;

    private static Logger logger = LoggerFactory.getLogger(CfiController.class);

    /**
     * 返回所有配置
     *
     * @return
     */
    public HashMap<String, String> getAll() {
        HashMap<String, String> map = new HashMap<>();
        List<CfgApiConfig> list = cfgApiConfigMapper.findAll();
        for (CfgApiConfig cfgApiConfig : list) {
            map.put(cfgApiConfig.getKey(), cfgApiConfig.getValue());
        }
        return map;
    }

    /**
     * 根据pid获取区域
     *
     * @param pid
     * @return
     */
    public List<CfgRegion> getCfgRegionByPid(Integer pid) {
        return cfgRegionMapper.findCfgRegionByPid(pid);
    }


    /**
     * 根据id获取区域
     *
     * @param id
     * @return
     */
    public CfgRegion getCfgRegionByid(Integer id) {
        return cfgRegionMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据Code获取区域
     *
     * @param code
     * @return
     */
    public CfgRegion getCfgRegionByCode(String code) {
        return cfgRegionMapper.findCfgRegionByCode(code);
    }


    /**
     * 获取所有的曲风类型
     *
     * @return
     */
    public List<CfgSongType> getCfgSongTypeALL() {
        return cfgSongTypeMapper.findAll();
    }


    /**
     * 根据id查询曲风类型
     *
     * @param id
     * @return
     */
    public CfgSongType getCfgSongTypeById(Integer id) {
        return cfgSongTypeMapper.selectByPrimaryKey(id);
    }


    /**
     * 根据name查询曲风类型
     *
     * @param name
     * @return
     */
    public CfgRegion getCfgRegionByName(String name) {
        return cfgRegionMapper.findCfgRegionByName(name);
    }


    /**
     * 获取app对应banner
     *
     * @return
     */
    public List<Map<String, Object>> getCfgAppBanner() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<CfgAppBanner> cfgAppBannerList = cfgAppBannerMapper.findCfgAppBanner(new Date());
        for (CfgAppBanner cfgAppBanner : cfgAppBannerList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("title", cfgAppBanner.getBannerTitle());
            resultMap.put("appImage", cfgAppBanner.getAppImageUrl());
            resultMap.put("type", cfgAppBanner.getType());
            resultMap.put("linkUrl", cfgAppBanner.getLinkUrl());
            resultMap.put("foreignId", cfgAppBanner.getForeignId());
            resultList.add(resultMap);
        }
        return resultList;
    }

    /**
     * 获取web对应的banner
     *
     * @param bannerType
     * @return
     */
    public List<Map<String, Object>> getCfgWebBanner(String bannerType) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<CfgWebBanner> cfgWebBannerList = cfgWebBannerMapper.findCfgWebBanner(new Date(), bannerType);
        for (CfgWebBanner cfgWebBanner : cfgWebBannerList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("title", cfgWebBanner.getBannerTitle());
            resultMap.put("webImage", cfgWebBanner.getWebImageUrl());
            resultMap.put("type", cfgWebBanner.getType());
            resultMap.put("foreignId", cfgWebBanner.getForeignId());
            resultMap.put("buttonText", cfgWebBanner.getButtonText());
            resultList.add(resultMap);
        }
        return resultList;
    }

    /**
     * 获取对应的终端获取配置
     *
     * @param terminalType
     * @return
     */
    public CfgAppConfig findCfgAppConfig(String terminalType) {
        return cfgAppConfigMapper.findByTerminalType(terminalType);
    }

    /**
     * 根据配置名获取配置
     *
     * @param key
     * @return
     */
    public CfgApiConfig findCfgApiConfigByKey(String key) {
        return cfgApiConfigMapper.findByKey(key);
    }

    /**
     * 获取众筹申请配置
     *
     * @return
     */
    public Map<String, List<Map>> getCfgApplyCrowdfundingItemsList() {
        Map<String, List<Map>> map = new HashMap<>();
        List<Map> itemsList = cfgApplyCrowdfundingItemsMapper.findAllItems();
        for (Map map1 : itemsList) {
            List<Map> list = map.get(map1.get("itemName"));
            if (list != null) {
                list.add(map1);
            } else {
                List<Map> newList = new ArrayList<>();
                newList.add(map1);
                map.put(map1.get("itemName").toString(), newList);
            }
        }
        List<Map> rateList = cfgApplyCrowdfundingItemsMapper.findItemsByItemName("exchange_rate");
        map.put("exchange_rate", rateList);
        return map;
    }

    /**
     * 查询cityId
     *
     * @param cityId
     * @return
     */
    public CfgChinaCityList findCfgChinaCityListByCityId(Integer cityId) {
        return cfgChinaCityListMapper.findByCityId(cityId);
    }



}
